package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Passwd;

@Repository
public interface PasswdDao extends JpaRepository<Passwd, Integer> {

    @Transactional
    @Modifying
    int deleteByPasswdId(Integer passwdId);

    @Transactional
    @Modifying
    int deleteByPasswdIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT p.*,u.account as userName,u.nickName FROM passwd p LEFT JOIN users u on p.user_Id = u.user_Id")
    List<Map<String, Object>> queryAllPasswdAndUser();

    List<Passwd> findByPasswdId(Integer passwdId);

    @Query(nativeQuery = true, value = "SELECT p.*,u.account as userName,u.nickName \r\n"
            + "		FROM passwd p INNER JOIN users u ON p.user_Id = u.user_Id\r\n"
            + "		WHERE u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT p.*,u.account as userName,u.nickName \r\n"
            + "		FROM passwd p INNER JOIN users u ON p.user_Id = u.user_Id\r\n"
            + "		WHERE u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

//	int resetPasswdByIds(List<Integer> listId);

}
